package com.eureka.pay.configurer.properties;

import com.alibaba.fastjson.JSONObject;
import com.eureka.bclass.util.StrUtil;
import com.eureka.pay.enums.BusinessTypeEnum;
import com.eureka.pay.enums.ClientEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

/**
 * @Description: 预支付信息
 * @Author: lixing
 * @Date: 2021/7/9 9:27
 */
@Component
public class AdvancePaymentPropertiesConfig {
    /** 微信、支付宝相关配置参数 */
    @Autowired
    private AliBabaLhgyyProperties aliBabaLhgyyProperties;
    @Autowired
    private WeChatLhgyyProperties weChatLhgyyProperties;

    /** 测试金额，单位：元 */
    @Value("${aggregatePay.tesPayMoney}")
    private String aggregatePayTesPayMoney;

    /**
     * @Description 根据聚合支付业务类型读取相关参数
     * @Author lixing
     * @Date 2021/7/8 11:13
     * @Param [businessType 100000 前3位100表示项目编号，后3位000表示业务场景编号]
     * @return com.alibaba.fastjson.JSONObject
     */
    public AdvancePaymentProperties getParamsByBusinessType(String client, String businessType, long userId) throws UnsupportedEncodingException {
        // 默认设置的支付金额单位为元
        BigDecimal totalAmount = null;
        if (StrUtil.isEffective(aggregatePayTesPayMoney)) {
            totalAmount = new BigDecimal(aggregatePayTesPayMoney);
        } else {
            totalAmount = new BigDecimal("0.1");
        }
        if (client.equals(ClientEnum.WECHAT.getClient())) {
            // TODO 微信支付金额单位为：分，支付宝支付金额单位为：元
            totalAmount = totalAmount.multiply(new BigDecimal("100"));
        }
        AdvancePaymentProperties advancePaymentProperties = new AdvancePaymentProperties();
        if (businessType.equals( BusinessTypeEnum.UP_COMPANY_USER.getBusinessType() )) {
            advancePaymentProperties.setBusinessType(businessType);
            // 订单参数
            advancePaymentProperties.setSubject("升级企业用户");
            advancePaymentProperties.setTotalAmount(StrUtil.formatNumStr(totalAmount.toString(), 0));
            advancePaymentProperties.setBody("升级企业用户");
            advancePaymentProperties.setProductId(businessType);
            // 公共参数
            JSONObject passbackParamsObj = new JSONObject();
            passbackParamsObj.put("client", client);
            passbackParamsObj.put("businessType", businessType);
            passbackParamsObj.put("userId", userId);
            //advancePaymentProperties.setPassbackParams(URLEncoder.encode(passbackParamsObj.toString(), "UTF8"));
            advancePaymentProperties.setPassbackParams(passbackParamsObj.toString());
            //
            advancePaymentProperties.setWechatAppId(weChatLhgyyProperties.getAppId());
            advancePaymentProperties.setWechatAppSecret(weChatLhgyyProperties.getAppSecret());
            advancePaymentProperties.setWechatMchId(weChatLhgyyProperties.getMchId());
            advancePaymentProperties.setWechatKey(weChatLhgyyProperties.getKey());
            advancePaymentProperties.setWechatNotifyUrl(weChatLhgyyProperties.getNotifyUrl());
            //
            advancePaymentProperties.setAlibabaAppId(aliBabaLhgyyProperties.getAppId());
            advancePaymentProperties.setAlibabaSignType(aliBabaLhgyyProperties.getSignType());
            advancePaymentProperties.setAlibabaRsaPrivateKey(aliBabaLhgyyProperties.getRsaPrivateKey());
            advancePaymentProperties.setAlibabaRsaPublicKey(aliBabaLhgyyProperties.getRsaPublicKey());
            advancePaymentProperties.setAlibabaNotifyUrl(aliBabaLhgyyProperties.getNotifyUrl());
            advancePaymentProperties.setAlibabaReturnUrl(aliBabaLhgyyProperties.getReturnUrl());
            advancePaymentProperties.setAlibabaGatewayUrl(aliBabaLhgyyProperties.getGatewayUrl());
            advancePaymentProperties.setAlibabaCharset(aliBabaLhgyyProperties.getCharset());
            advancePaymentProperties.setAlibabaFormat(aliBabaLhgyyProperties.getFormat());
        }
        return advancePaymentProperties;
    }
}
